package com.blackcat.mybatisplus.wrapper.config;

import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.wrapper.ObjectWrapper;
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
* mybatis返回字段转小写
* @author zhanghui
* @since 2023/4/20 09:23
*/
@Component
public class SqlFieldToLowerCaeFactory implements ObjectWrapperFactory {
    @Override
    public boolean hasWrapperFor(Object o) {
        return o instanceof Map;
    }

    @Override
    public ObjectWrapper getWrapperFor(MetaObject metaObject, Object o) {
        return new SqlFieldToLowerCase(metaObject, (Map) o);
    }

    /**
     *  使用该方式object-wrapper-factory: com.demo.config.SqlFieldToLowerCaeFactory 会报类型转换异常所以使用下面这种方式
     */
    @Bean
    public ConfigurationCustomizer mybatisConfigurationCustomizer() {
        return configuration -> configuration.setObjectWrapperFactory(new SqlFieldToLowerCaeFactory());
    }

}